<?php
namespace app\common\model;
use app\common\traits\Models;
use think\Model;
use think\facade\{
    App,Request,Config
};
class Company extends Model
{
    use Models;
    protected $readonly = [];
    protected $auto     = [];
    protected $insert   = [];
    protected $update   = [];

    //业务员
    public function seller()
    {
        return $this->hasMany('seller','company_id','id');
    }

    //公司
    public function company()
    {
        return $this->belongsTo('company','company_id','id');
    }


    //用户登录
    public function login($username, $password){
        $user = $this::where('username',$username)->find();
        if (empty($user) ) {
            $this->error = '用户不存';
            return false;
        }else{
            if((int)$user->status !== 0){
                $this->error = '用户被禁用';
                return false;
            }elseif($user->password!=com_password($password)){
                $this->error = '用户密码不正确';
                return false;
            }
        }
        /* 更新用户登录token */
        $token = com_token($user->id);
        $user->token = $token;
        $user->save();
        return $token;
    }

    /**
     * 获取用户信息
     * @param  string $token [登录token]
     * @return [type]        [description]
     */
    public function getInfoBytoken($token=''){
        $rs = $this::get(function ($query) use ($token) {
            $query->where([['token','=', $token]]);
        });
        if($rs)
            return $rs->getData();
        return [];
    }
}
